package aftersale

import (
	"context"

	"mall/common/utils/pointy"
	"mall/service/backend/api/internal/logic/utils"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	"mall/service/order/rpc/orderclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAfterSaleByIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 通过ID获取售后
func NewGetAfterSaleByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAfterSaleByIdLogic {
	return &GetAfterSaleByIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetAfterSaleByIdLogic) GetAfterSaleById(req *types.IDReq) (*types.AfterSaleInfoResp, error) {
	if req.Id == 0 {
		return nil, errorx.NewInvalidArgumentError(i18n.ValidationError)
	}

	data, err := l.svcCtx.OrderRpc.BackendGetAfterSaleById(l.ctx, &orderclient.AfterSaleIdReq{
		Id:          req.Id,
		WithMedia:   pointy.GetPointer(uint32(1)),
		WithAddress: pointy.GetPointer(uint32(1)),
		WithPayment: pointy.GetPointer(uint32(1)),
	})
	if err != nil {
		return nil, err
	}

	option := utils.GetOptionByStatus(pointy.GetPointer(uint32(*data.Status)))
	var media []*types.MediaInfo
	if len(data.Media) > 0 {
		for _, v := range data.Media {
			var url string
			if v.Url != nil && len(*v.Url) > 0 {
				url = *v.Url
			}
			if len(url) == 0 && v.Uri != nil && len(*v.Uri) > 0 {
				url = *v.Uri
			}
			filename, mimeType, metaData := utils.ParseMetaData(v.MetaData)
			media = append(media, &types.MediaInfo{
				Uid:       v.Uid,
				Id:        v.Id,
				Thumbnail: v.Thumbnail,
				Order:     v.Order,
				Url:       pointy.GetPointer(url),
				Name:      pointy.GetPointer(filename),
				MimeType:  pointy.GetPointer(mimeType),
				AltText:   v.AltText,
				MetaData:  pointy.GetPointer(metaData),
			})
		}
	}

	var payments []*types.OrderPaymentInfo
	if len(data.Payment) > 0 {
		for _, v := range data.Payment {
			payments = append(payments, &types.OrderPaymentInfo{
				Id:      v.Id,
				OrderId: v.OrderId,
				No:      v.No,
				Amount:  v.Amount,
				Type:    v.Type,
				Name:    v.Name,
				Code:    v.Code,
				PaidAt:  v.PaidAt,
			})
		}
	}

	var addresses []*types.OrderAddressInfo
	if len(data.Addresses) > 0 {
		for _, v := range data.Addresses {
			addresses = append(addresses, &types.OrderAddressInfo{
				Id:         v.Id,
				OrderId:    v.OrderId,
				UserId:     v.UserId,
				Consignee:  v.Consignee,
				Mobile:     v.Mobile,
				Province:   v.Province,
				City:       v.City,
				District:   v.District,
				Address:    v.Address,
				PostalCode: v.PostalCode,
				RegionCode: v.RegionCode,
				Label:      v.Label,
			})
		}
	}

	return &types.AfterSaleInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: &types.AfterSaleInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:         data.Id,
				CreatedAt:  data.CreatedAt,
				UpdatedAt:  data.UpdatedAt,
				DeletedAt:  data.DeletedAt,
				CanAccept:  option.CanAccept,
				CanReject:  option.CanReject,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			AfterSaleSn:      data.AfterSaleSn,
			OrderId:          data.OrderId,
			OrderItemId:      data.OrderItemId,
			OrderSn:          data.OrderSn,
			ProductId:        data.ProductId,
			ProductCode:      data.ProductCode,
			ProductTitle:     data.ProductTitle,
			ProductSkuId:     data.ProductSkuId,
			ProductSkuCode:   data.ProductSkuCode,
			ProductSku:       data.ProductSku,
			SkuThumbUrl:      data.SkuThumbUrl,
			ProductPrice:     data.ProductPrice,
			Quantity:         data.Quantity,
			UserId:           data.UserId,
			NickName:         data.NickName,
			Mobile:           data.Mobile,
			ReturnAddressId:  data.ReturnAddressId,
			AfterSaleType:    data.AfterSaleType,
			RefundType:       data.RefundType,
			ReturnType:       data.ReturnType,
			RefundAmount:     data.RefundAmount,
			RefundDetails:    data.RefundDetails,
			Remark:           data.Remark,
			ExpressSn:        data.ExpressSn,
			ReasonForRefusal: data.ReasonForRefusal,
			Status:           data.Status,
			ProcessTime:      data.ProcessTime,
			Addresses:        addresses,
			Media:            media,
			Payments:         payments,
		},
	}, nil
}
